/*
 * @# LoginAction.java 2011-11-15 下午09:06:57
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.sen.pro.switches.admin.login.action;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sen.common.security.SimpleImageCaptchaServlet;
import com.sen.common.struts.BaseAction;
import com.sen.common.util.Feedback;
import com.sen.pro.switches.core.cfg.AppPropReader;
import com.sen.pro.switches.core.interceptor.SessionFetcher;

/**
 * 登陆后台Action
 * 
 * @author tian qingsen
 */
@Scope("prototype")
@Controller("loginAction")
@Namespace("/admin")
public class LoginAction extends BaseAction {

	private static final long serialVersionUID = -3285731054044949709L;
	private static Logger LOG = Logger.getLogger(LoginAction.class);

	private static final String SYS_USER_NAME = AppPropReader.getPropertie("administration.username");
	private static final String SYS_USER_PWD = AppPropReader.getPropertie("administration.password");

	// 用户名
	private String userName;
	// 密码
	private String userPwd;
	// 验证码
	private String captchaResponse;

	@Action(value = "login", results = { @Result(name = SUCCESS, location = "/WEB-INF/view/admin/login.jsp") })
	public String login() {
		return SUCCESS;
	}

	@Action("checkLogin")
	public String checkLogin() {
		Feedback feedback = checkLoginValidation();
		writeJson(feedback);
		return NONE;
	}

	private Feedback checkLoginValidation() {
		if (!SimpleImageCaptchaServlet.validate(request, "sessionChallenge", captchaResponse)) {
			return Feedback.fail("验证码不正确").setType("captcha");
		}

		if (StringUtils.isBlank(userName) || StringUtils.isBlank(userPwd) || !SYS_USER_NAME.equals(userName)
				|| !SYS_USER_PWD.equals(userPwd)) {
			return Feedback.fail("用户名或密码错误");
		}

		SessionFetcher.from(session).setUserLogined(userName);
		if (LOG.isInfoEnabled()) {
			LOG.info("登陆成功，时间：" + DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss"));
		}
		return Feedback.success("通过");
	}

	@Action(value = "manage", results = { @Result(name = SUCCESS, location = "/WEB-INF/view/admin/index.jsp") }, interceptorRefs = { @InterceptorRef("switches-needsession-stack") })
	public String manage() {

		return SUCCESS;
	}

	@Action(value = "logout", results = { @Result(name = SUCCESS, type = "redirect", location = "login", 
			params = { "namespace", "/admin" }) })
	public String logout() {
		session.clear();
		return SUCCESS;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public String getCaptchaResponse() {
		return captchaResponse;
	}

	public void setCaptchaResponse(String captchaResponse) {
		this.captchaResponse = captchaResponse;
	}

}
